package com.cslg.ac.demo.controller.user;
import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.service.user.UserService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@PropertySource(value = {"classpath:user.properties"},encoding="utf-8")
public class UserController {

    @Autowired
    UserService userService;

    @Value("${nameLength}")
    int nameLength;
    @Value("${userNameLength}")
    int userNameLength;
    @Value("${passwordLength}")
    int passwordLength;
    @Value("${teamType}")
    int teamType;
    @Value("${personType}")
    int personType;
    @Value("${userType}")
    String userType;
    @Value("${phoneLength}")
    int phoneLength;


    @RequestMapping(value = "/user/logout",method = RequestMethod.POST)
    public void logout(HttpSession session,PrintWriter out){
        session.removeAttribute("user");
        ResponseUtil.responseCode(out,ResponseCode.REQUEST_SUCCESSED);
    }

    /**
     *
     * @param userGet
     * @param out
     * @param session
     */
    @RequestMapping(value = "/user/register",method = RequestMethod.POST)
    public void register(User userGet, PrintWriter out, HttpSession session){


            if(!userGet.registerValid(userNameLength,passwordLength)){
                ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
                return;
            }
            int code;
            Integer id;
            try {
                code = userService.register(userGet);
                if(code != ResponseCode.REQUEST_SUCCESSED)
                {
                    ResponseUtil.responseCode(out, code);
                    return;
                }
                id = userService.getId(userGet.getUserName());
            } catch (Exception e) {
                e.printStackTrace();
                ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
                return;
            }
        userGet.setId(id);
        session.setAttribute("user",userGet);
        Map<String,Object> param = new HashMap<>(1);
        param.put("id",id);
        ResponseUtil.response(out,code,param);

        }



    @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    public void login(User userGet, PrintWriter out, HttpSession httpSession){
        if(!userGet.registerValid(userNameLength,passwordLength)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code = 0;
        try {
            code = userService.login(userGet);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
        }
        if(code != ResponseCode.REQUEST_SUCCESSED)
        {
            ResponseUtil.responseCode(out, code);
            return;
        }
        Integer id = userService.getId(userGet.getUserName());
        userGet.setId(id);
        httpSession.setAttribute("user",userGet);
        Map<String,Object> param = new HashMap<>(1);
        param.put("id",id);
        ResponseUtil.response(out,code,param);

    }


}
